package com.example.homeworkdev.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.homeworkdev.R;
import com.example.homeworkdev.adapter.MessageAdapter;
import com.example.homeworkdev.entity.Message;
import com.example.homeworkdev.pages.MessageInfoActivity;

import java.util.ArrayList;
import java.util.List;


public class MessageFragment extends Fragment {

    View view;

    ListView listView;
    MessageAdapter adapter;
    List<Message> itemList;

    public MessageFragment() {
        // Required empty public constructor
        itemList = new ArrayList<>();
        itemList.add(new Message(R.drawable.cxk,"菜虚坤","出来打篮球了！","12:00",false));
        itemList.add(new Message(R.drawable.yangkun,"四川杨坤","我问你礼貌吗?","12:00",false));
        itemList.add(new Message(R.drawable.yujie,"你的雨姐","邀请你加入视频通话!","12:00",false));
        itemList.add(new Message(R.drawable.manbo,"曼波","哈基米,你这家伙!","12:00",false));
        itemList.add(new Message(R.drawable.doro,"桃乐丝","doro统治世界!","12:00",false));
        itemList.add(new Message(R.drawable.laoda,"牢大","这并不好笑,孩子","12:00",false));
        itemList.add(new Message(R.drawable.sunxiaochuan,"天皇孙笑川","请你们尊重一下逝者好吗","12:00",false));
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // 若视图不为空则加载fragment布局
        if(view == null){
            view = inflater.inflate(R.layout.fragment_message, container, false);
        }

        //初始化listView
        listView = view.findViewById(R.id.message_list);
        //设置适配器
        Context context = requireContext();
        adapter = new MessageAdapter(context,R.layout.list_message,itemList);
        listView.setAdapter(adapter);

        //点击跳转
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Message selectedPet = itemList.get(position);
                Intent intent = new Intent(context, MessageInfoActivity.class);
                intent.putExtra("img", selectedPet.getImg());
                intent.putExtra("name", selectedPet.getName());
                intent.putExtra("message", selectedPet.getMesssage());
                startActivity(intent);
            }
        });



        return view;
    }

}